﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using yq.Exchange;

namespace yq
{
    /// <summary>
    /// 调用Service方法，调用取决于配置文件
    /// </summary>
    public class GetService
    {
        /// <summary>
        /// 配置文件路径
        /// </summary>
        private const string _DefaultFilePath = "IoC.config";

        private static Dictionary<string, yq.Exchange.RefValue> _Dic;
        /// <summary>
        /// 配置文件缓存
        /// </summary>
        private static Dictionary<string, yq.Exchange.RefValue> Dic
        {
            get
            {
                if (_Dic == null)
                {
                    _Dic = Initialization();
                }

                return _Dic;
            }
        }

        /// <summary>
        /// 取得Service实例
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T Create<T>() where T : class
        {
            RefValue info = new RefValue();

            if (Dic.TryGetValue(typeof(T).FullName, out info))
            {
                ExchangeIBase exchange = null;
                if (info.Type == RefType.Wcf)
                {
                    exchange = new Wcf();
                }
                else
                {
                    exchange = new Reflection();
                }
                return  exchange.Create<T>(info);
            }
            else
            {
                throw new Exception("未配置交互方式，请检查" + _DefaultFilePath + "文件");
            }

        }


        /// <summary>
        /// 初始化配置文件
        /// </summary>
        /// <returns></returns>
        private static Dictionary<string, yq.Exchange.RefValue> Initialization()
        {
            string configFilePath = cn.Util.WebConfig.ConfigFilePath + _DefaultFilePath;

            var dics = new Dictionary<string, yq.Exchange.RefValue>();

            var root = XElement.Load(configFilePath, LoadOptions.None);
            var allElement = from obj in root.Elements()
                             select obj;

            foreach (XElement host in allElement)
            {
                RefType type = host.Name.LocalName == "Wcf" ? RefType.Wcf : RefType.Reflection;

                //反射路径
                string reflectionPath = host.Attribute(XName.Get("reflectionPath")).Value;

                //反射路径
                string web = host.Attribute(XName.Get("web"))== null ? "" : host.Attribute(XName.Get("web")).Value;

                //取类信息
                foreach (XElement cla in host.Elements())
                {
                    yq.Exchange.RefValue model = new yq.Exchange.RefValue()
                    {
                        Class = cla.Attribute(XName.Get("class")).Value,
                        Desc = cla.Attribute(XName.Get("desc")).Value,
                        Interface = cla.Attribute(XName.Get("interface")).Value,
                        ReflectionPath = reflectionPath,
                       Web = web,
                       Type = type
                    };

                    ////取属性信息
                    ////foreach (XElement excla in cla.Elements())
                    ////{
                    ////    yq.Exchange.Property propertymodel = new yq.Exchange.Property()
                    ////    {
                    ////        Name = cla.Attribute(XName.Get("name")).Value,
                    ////        Class = cla.Attribute(XName.Get("className")).Value,
                    ////    };
                    ////    model.Property.Add(propertymodel);
                    ////}

                    dics.Add(model.Interface, model);
                }

            }

            return dics;
        }
    }
}
